package com.loutick.basicforapp.service.impl;

import java.util.Calendar;
import java.util.Date;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.loutick.basicforapp.dao.TVerificationMapper;
import com.loutick.basicforapp.entity.TVerification;
import com.loutick.basicforapp.service.IVerificationService;
import com.loutick.basicforapp.util.VerificationCode;

@Service("verificationService")   
@Transactional
public class VerificationServiceImpl implements IVerificationService {
	
	@Resource
	private TVerificationMapper verificationDao;
	
	public int insertVerificationCode(TVerification record) {
		return this.verificationDao.insertSelective(record); 
	}

	public TVerification getVerificationCodeByPhoneNumber(TVerification record) {
		
		return this.verificationDao.selectValidCodeByPhoneNumber(record);
	}

	public int updateByInvalidCode(Date invalidtime) {
		return this.verificationDao.updateByInvalidtime(invalidtime);
	}

	public int updateByCodeId(TVerification record){
		
		return this.verificationDao.updateByPrimaryKeySelective(record);
	}
	
	public int insertVerificateionByPhoneNumber(String PhoneNumber){
		TVerification record = new  TVerification();
		record.setCodenumber(VerificationCode.createCode(true, 6));
		Calendar c = Calendar.getInstance();
		record.setCreatetime(c.getTime());
		c.add(Calendar.MINUTE, 30);
		record.setInvalidtime(c.getTime());
		record.setPhonenumber(PhoneNumber);
		return this.insertVerificationCode(record);
	}
	
	public int insertVerificateionByUserID(Integer userID,String PhoneNumber){
		TVerification record = new  TVerification();
		record.setCodenumber(VerificationCode.createCode(true, 6));
		Calendar c = Calendar.getInstance();
		record.setCreatetime(c.getTime());
		c.add(Calendar.MINUTE, 30);
		record.setInvalidtime(c.getTime());
		record.setUserid(userID);
		record.setPhonenumber(PhoneNumber);
		return this.insertVerificationCode(record);
	}
	
}
